import re

import scrapy

from scrapy_redis.spiders import RedisSpider
from lianjia_details.lianjia_index.items import LianjiaIndexItem

class DetailsSpider(RedisSpider):
    name = 'details'
    # allowed_domains = ['www.lianjia.com']
    # start_urls = ['http://www.lianjia.com/']

    # redis_key = ""
    redis_key = "index:requests"

    def __init__(self, *args, **kwargs):
        # print(f"redis:{self.redis_key}")
        super(DetailsSpider, self).__init__(*args, **kwargs)

    # def open_spider(self):
    # 	pass



    def parse(self, response):
        # selectors = Selector(text=response)
        # self.logger.debug(f"{response.}")

        items = response.meta["items"]
        try:
            tx = response.xpath('//*[@name="location"]/@content').extract_first()
            items['city'] = re.search(".*?city=(\w+)", tx).group(1)
            print("tx:", type(tx))
            items['position'] = re.search(".*?province=(\w+)", tx).group(1)

            # print(re.search(".*?province=(\w+)", tx).group(1))
            items['url'] = response.url  # 获取当前url
            items['title'] = response.xpath('//title/text()').extract()
            items['acreage'] = response.xpath('//*[@class="content__aside__list"]/li[2]/text()').extract()
            items['facilityList'] = [x.strip() for x in
                                     response.xpath('//*[@class="content__article__info2"]/li/text()').extract() if
                                     x.strip() != '']  # 基础设施
            items['description'] = ''.join(response.xpath('string(//*[@class="threeline"])').extract()).replace(
                '\n',
                '')  # 房子介绍
            items['basicInformation'] = [x.strip() for x in
                                         response.xpath(
                                             '//*[@class="content__article__info"]/ul/li/text()').extract()
                                         if
                                         x.strip() != '']  # 基本信息
            items['paymentMethod'] = response.xpath('//*[@class="table_row"]/li/text()').extract()[0]  # 付款方式
            items['rent'] = response.xpath('//*[@class="table_row"]/li/text()').extract()[1]  # 租金
            items['deposit'] = response.xpath('//*[@class="table_row"]/li/text()').extract()[2]  # 押金
            items['serviceCharge'] = response.xpath('//*[@class="table_row"]/li/text()').extract()[3]  # 服务费
            items['agencyFee'] = response.xpath('//*[@class="table_row"]/li/text()').extract()[4]  # 中介费
            items['label'] = response.xpath('//*[@class="content__aside--tags"]/i/text()').extract()
            yield items
        except AttributeError as A:
            self.logger.error(f"无法获取属性{A}")

